'use strict';

angular.module('snPaas')
.directive('spItemSelect',['baseUrl','vecProcess',function(baseUrl,vecProcess){
    return {
      restrict:'A',
      priority:-1,
      templateUrl:baseUrl.static+'template/directive/spItemSelect.html',
      replace:true,
      scope:{
         spValue: '=',
         spItemSelect: '=',
         spDisplay: '=',
         spId: '=',  //list id
         spImg: '=',
         spSelId: '=',  //selected id
         spFocus: '='
      },
      link:function postLink(scope,iElement,iAttrs){
        if(!scope.spValue){
          scope.spValue = [];
        }
        if(!scope.spItemSelect){
          scope.spItemSelect = [];
        }
        if(!scope.spDisplay){
          scope.spDisplay = 'name';
        }
        if(!scope.spId){
          scope.spId = 'id';
        }
        if(!scope.spImg){
          scope.spImg = 'img';
        }
        if(!scope.spSelId){
          scope.spSelId = 'id';
        }



        scope.expand = false;
        scope.baseUrl = baseUrl.static;
        scope.val = scope.spValue;
        scope.selList = [];

        scope.$watch('spValue',function(n,o){
          if(n!= o){
            filte();
            // deWatch();
          }
        });
        scope.$watch('spItemSelect',function(n,o){
          if(n!= o){
            init();
          }
        });
        var filte = function(){
          scope.spValue = vecProcess.getSelected(scope.spValue,scope.spSelId,scope.spItemSelect,scope.spId);
          console.log(scope.spValue);
        };
        var init = function(){
          if(scope.spValue.length>0){
            scope.spValue[0].focus = true;
            scope.spFocus = scope.spValue[0];

          }
        };
        
        scope.toggle = function(){
          scope.expand = !scope.expand;
        };
        scope.select = function(item,e){
          var id = item[scope.spId];
          for(var i=0,l= scope.spValue.length;i<l;i++){
            if(scope.spValue[i][scope.spId] ==id){
              e.stopPropagation();
              return;
            }
          }
          var id = item[scope.spId];
          item[scope.spSelId] = id;
          item.selected = true;
          if(!scope.spFocus){
            item.focus =true;
            scope.spFocus = item;
          }
          scope.spValue.push(item);
        };
        scope.itemClick = function(item){
          if(scope.spFocus != item){
            scope.spFocus.focus = false;
            item.focus = true;
            scope.spFocus = item;
          }
        };
        scope.deleteItem = function(item){
          var tem = [];
          var id = item[scope.spSelId];
          for(var i=0,l= scope.spValue.length;i<l;i++){
            if(scope.spValue[i][scope.spSelId] !=id){
              tem.push(scope.spValue[i]);
            }
            else{
              if(scope.spValue[i].linked){
                scope.spValue[i].linked.selected = false;
                scope.spValue[i].linked = null;
              }
              else{
                scope.spValue[i].selected = false;
              }
              
            }
          }
          if(item.focus){
            item.focus =false;
            if(tem[0]){
              tem[0].focus = true;
              scope.spFocus = tem[0];
            }
            else{
              scope.spFocus = false;
            }
            
          }
          scope.spValue = tem;
        };
      }
   }

}]);